//
//  ChessBoard.swift
//  Chess5
//
//  Created by apple on 2017/3/20.
//  Copyright © 2017年 apple. All rights reserved.
//

import Foundation
import UIKit
import CoreGraphics

public class ChessBoard: UIView {
    
    var xDelta: Int
    var yDelta: Int
    
    var xMax: Int
    var yMax: Int
    var lastStep: Position
    var newImage: UIImageView
    var cImage: UIImage?
    var hImage: UIImage?
    
    let newPath = Bundle.main.path(forResource: "blacknew", ofType: "png")!
    let redPath = Bundle.main.path(forResource: "red_60", ofType: "png")!
    let bulePath = Bundle.main.path(forResource: "blue_40", ofType: "png")!
    let N = 15
    
    required public init?(coder aDecoder: NSCoder) {
        xDelta = 50
        yDelta = 50
        xMax = 0
        yMax = 0
        lastStep = Position(x: -1,y: -1)
        
        let image = UIImage.init(named: newPath)
        newImage = UIImageView.init(image:image)
        newImage.frame = CGRect(x: 0, y: 0, width: xDelta, height: yDelta)
        newImage.layer.opacity = 0.5
        
        cImage = UIImage.init(named: bulePath)
        
        hImage = UIImage.init(named: redPath)

        super.init(coder: aDecoder)
    }

    public func go(player: Role,x: Int,y: Int,step: Int) {
        let rect = CGRect(x: x * xDelta,y: y * yDelta, width: xDelta,height: yDelta)

        var image: UIImageView
        if player == Role.computer {
            image = UIImageView.init(image: cImage)
        }
        else {
            image = UIImageView.init(image: hImage)
        }
        
        image.frame = rect
        let stepInfo = UILabel.init()
        stepInfo.textColor = UIColor.white
        stepInfo.font = UIFont.systemFont(ofSize: 16)

        stepInfo.text = String.init(format: "%d", step)
        stepInfo.sizeToFit()
        let size = stepInfo.frame.size
        let startX = image.frame.width / 2 - size.width / 2
        let startY = image.frame.height / 2 - size.height / 2
        stepInfo.frame = CGRect(x: startX, y: startY, width: size.width, height: size.height)
        image.addSubview(stepInfo)
        
        if newImage.superview != nil {
            newImage.removeFromSuperview()
        }
        image.addSubview(newImage)
        
        self.addSubview(image)
        
    }
    public override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        
        context?.setLineWidth(2.0)
        context?.setStrokeColor(UIColor.black.cgColor)
        
        xDelta = Int(rect.size.width / 15)
        yDelta = Int(rect.size.height / 15)
        xMax = Int(rect.size.width)
        yMax = Int(rect.size.height)
        
        var xOffset = 0
        var yOffset = 0
        let path = CGMutablePath()
        for  i in 0..<16 {
//            let x:CGFloat = CGFloat(i)
            xOffset = Int(xDelta) * i
            yOffset = Int(yDelta) * i
            path.move(to: CGPoint(x: xOffset,y: 0))
            path.addLine(to: CGPoint(x: xOffset,y: yMax))
            
            context?.addPath(path)
            context?.drawPath(using: .stroke)

            path.move(to: CGPoint(x: 0,y: yOffset))
            path.addLine(to: CGPoint(x: xMax, y: yOffset))
            context?.addPath(path)
            context?.drawPath(using: .stroke)
            
        }
    }
}
